package com.ellricky.baselib.ui.clue

import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.EditText
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.andr.common.util.ResUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.ui.ActiveDocActivity
import com.ellricky.baselib.ui.ErCodeActivity
import com.ellricky.baselib.ui.base.BaseRefreshFragment
import com.ellricky.baselib.ui.base.BaseTabVpFragment
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.widget.LabelsView

/**
 * 课程顾问：我的线索
 */
class MyClueFragment : BaseTabVpFragment() {

    private lateinit var fragments: ArrayList<Fragment>
    private var position: Int = 0

    companion object {
        fun newInstance(): MyClueFragment {
            val fragment = MyClueFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }
    }


    override fun initUIView(root: View) {
        super.initUIView(root)
        setLeftIb(R.drawable.base_icon_ercode)
        mBtnRightLeft.text = "课程活动"
        mBtnRightLeft.visibility = View.VISIBLE
        val params = mBtnRightLeft.layoutParams as? RelativeLayout.LayoutParams
        params?.addRule(RelativeLayout.CENTER_VERTICAL)
        mBtnRightLeft.layoutParams = params
        mBtnRightLeft.setTextColor(ContextCompat.getColor(requireContext(), R.color.white))
        mBtnRightLeft.background =
            ContextCompat.getDrawable(requireContext(), R.drawable.base_bg_btn_black_bg)
        setRightBtn(0)
    }

    override fun onTabSelect(position: Int) {
        this.position = position
        val labelView = llCommon.getChildAt(1)
        if (position == 1) {
            labelView.visibility = View.VISIBLE
        } else {
            labelView.visibility = View.GONE
        }
    }

    override fun customView(): View {
        val view = layoutInflater.inflate(R.layout.layout_club_intent, null)
        val label = view.findViewById<LabelsView>(R.id.label)
        label.setLabels(arrayListOf("A", "B", "C", "D"))
        label.setOnLabelSelectChangeListener { label, data, isSelect, position ->
            val fragment = fragments[1] as MyClueChildFragment
            if (isSelect) {
                fragment.refresh(data as String)
            } else {
                fragment.refresh("")
            }
        }
        return view
    }

    override fun searchView(): View {
        val view = layoutInflater.inflate(R.layout.layout_search, null)
        val etSearch = view.findViewById<EditText>(R.id.etSearch)
        etSearch.setOnEditorActionListener { v, actionId, event ->
            if (actionId == 0 || actionId == 3) {
                val fragment = fragments[position] as MyClueChildFragment
                fragment.search(etSearch.text.toString())
            }
            true
        }
        return view
    }

    override fun getFragmentList(): MutableList<Fragment> {
        fragments = ArrayList()
        fragments.add(MyClueChildFragment.newInstance(BaseConstant.CLUE_TYPE_WAIT))
        fragments.add(MyClueChildFragment.newInstance(BaseConstant.CLUE_TYPE_WISH))
        fragments.add(MyClueChildFragment.newInstance(BaseConstant.CLUE_TYPE_TY))
        fragments.add(MyClueChildFragment.newInstance(BaseConstant.CLUE_TYPE_FINISH))
        return fragments
    }

    override fun getTabTitleList(): List<String> {
        return ResUtil.getStrArr(mContext, R.array.MyClueTab).asList()
    }

    override fun getTitleRes(): Int {
        return R.string.base_my_clue
    }

    override fun onClickView(v: View?) {
        super.onClickView(v)
        if (v == mIbBack) EAppUtil.jumpPageWithNoParam(this, ErCodeActivity::class.java)
    }

    override fun onClickBtnRight() {
        super.onClickBtnRight()
        EAppUtil.jumpPageWithNoParam(this, AddClueActivity::class.java)
    }

    override fun onClickBtnRightLeft() {
        super.onClickBtnRightLeft()
        EAppUtil.jumpCommonFragment(requireContext(), BaseConstant.FRAGMENT_ACTIVE_DOC, null)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode > 0) {
            val curFrg = tabVp.curFrg as MyClueChildFragment
            curFrg.loadDataOnInit()
        }
    }
}
